﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;
using EFong.View.Customer;
using System.IO;
using EFong.View.Base;

namespace EFong.View.TouringParty
{
    public partial class Frm_TouringParty : BasicMdiChildForm
    {
        private Proxy.BusinessServiceProxy _businessProxy {get{ return Proxy.BusinessServiceProxy.GetInstance();}}
        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}

        public Frm_TouringParty()
        {
            InitializeComponent();

            txt_Name.ReadOnly(true);
            txt_Customer.ReadOnly(true);
            txt_OrderRemark.ReadOnly(true);
            txt_CPerson.ReadOnly(true);
            txt_CPersonTel.ReadOnly(true);
            spe_AdultQty.ReadOnly(true);
            spe_ChildQty.ReadOnly(true);
            //txt_ChildType.ReadOnly(true);

            this.bbt_Void.Visibility = AppParameters.IsAdRole ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;
        }

        protected override void InitFields()
        {
        }

        protected override void Events()
        {
            this.gv_Orders.FocusedRowChanged += (s, e) =>
            {
                Contract.RTouringParty order = this.gv_Orders.GetRow(e.FocusedRowHandle) as Contract.RTouringParty;

                if (order != null)
                {
                    Order = _masterProxy.GetTouringPartyByID(order.ID).ReturnValue;
                }
                else
                {
                    Order = null;
                }
            };
            this.btn_CustomerData.Click += (s, e) =>
            {
                browserCustomerData();
            };

            this.btn_DownloadFile.Click += (s, e) =>
                {
                    if (Order == null)
                        return;
                    if (Order.Document == null)
                        return;

                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.FileName = Order.DocumentName;
                    saveFileDialog.Filter = "所有文件(*.*)|*.*";
                    DialogResult result = saveFileDialog.ShowDialog();
                    if (result != DialogResult.OK)
                        return;

                    UIOperManager.SaveData(() => { return true; },
                        () =>
                        {
                            writeFile(saveFileDialog.FileName, Order.Document);
                            return true;
                        },
                        "下载成功.");
                };


            this.gv_TouringPartyItems.CustomDrawCell += (s, e) =>
            {
                if (e.Column != colUM)
                    return;

                Contract.TouringPartyItem item = this.gv_TouringPartyItems.GetRow(e.RowHandle) as Contract.TouringPartyItem;

                if (string.IsNullOrEmpty(item.Type))
                    item.UM = string.Empty;

                else
                {
                    switch (item.Type)
                    {
                        case "车票":
                            item.UM = "张";
                            break;
                        case "门票":
                            item.UM = "张";
                            break;
                        case "酒店":
                            item.UM = "间";
                            break;
                        case "租车":
                            item.UM = "辆";
                            break;
                        case "用餐":
                            item.UM = "人";
                            break;
                        case "其它":
                            item.UM = "份";
                            break;
                        default:
                            break;
                    }
                }
            };
        }

        protected override void LoadDatas()
        {
            bds_TouringParty.BindingData(_masterProxy.GetAllTouringParty().ReturnValue);

            SetAuthorities();
        }

        protected void SetAuthorities()
        {
            foreach (Contract.RoleAuthorizedItem roleAuthorizedItem in AppParameters.LoginUser.Role.Authorities)
            {
                switch (roleAuthorizedItem.AuthorizedItem.Name)
                {
                    case "自助团":
                        bbt_Add.Enabled = roleAuthorizedItem.Allow;
                        bbt_Edit.Enabled = roleAuthorizedItem.Allow;
                        bbt_Void.Enabled = roleAuthorizedItem.Allow;
                        break;
                }
            }
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Add":
                    this.add();
                    break;
                case "bbt_Edit":
                    this.edit();
                    break;
                case "bbt_Void":
                    this.voidOrder();
                    break;
                case "bbtPrint":
                    this.printOrder();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        private void printOrder()
        {
            if (Order == null)
                return;

            BasicPrinterForm frm = new BasicPrinterForm();
            frm.Text = "打印订单";

            Print.Rpt_TourParty report = new Print.Rpt_TourParty();
            report.Order = _masterProxy.GetTouringPartyByID(Order.ID).ReturnValue;

            frm.Report = report;

            FormOpener.ShowDialog(frm);
        }

        private void voidOrder()
        {
            bool isSure = false;
            MsgBox.Question("确定将订单作废?", () => { isSure = true; }, null);
            if (!isSure)
                return;

            if (Order == null)
                return;

            Frm_Void frm = new Frm_Void(Order);
            frm.Text = "订单作废";
            if (FormOpener.ShowDialog(frm) == DialogResult.OK)
                bds_TouringParty.BindingData(_masterProxy.GetAllTouringParty().ReturnValue);
        }

        private void add()
        {
            Frm_TouringPartyEdit frm = new Frm_TouringPartyEdit();
            frm.Text = "增加订单";
            frm.ViewState = ViewStates.ADD;
            frm.Load += (s, e) =>
            {
                frm.Order = new Contract.TouringParty();
            };
            FormOpener.ShowDialog(frm);

            if (frm.ReturnValue != null && this.bds_TouringParty.DataSource is List<Contract.RTouringParty>)
            {
                this.bds_TouringParty.Insert(0, frm.ReturnValue);
            }
        }

        private void edit()
        {
            if (Order == null)
                return;

            Frm_TouringPartyEdit frm = new Frm_TouringPartyEdit();
            frm.Text = "修改订单资料";
            frm.ViewState = ViewStates.EDIT;
            frm.Load += (s, e) =>
            {
                frm.Order = Order;
            };
            if (FormOpener.ShowDialog(frm) == DialogResult.OK)
            {
                this.gv_Orders.BeginUpdate();
                this.gv_Orders.EndUpdate();
                Order = frm.Order;
            }
        }

        private void browserCustomerData()
        {
            if (Order == null)
                return;

            Frm_CustomerEdit frm = new Frm_CustomerEdit();
            frm.Text = "客户详细资料";
            frm.ViewState = ViewStates.BROWSER;
            frm.Load += (s, e) =>
            {
                frm.CurCust = Order.Customer;
            };
            FormOpener.ShowDialog(frm);
        }

        #region Order
        //selected TouringParty
        private Contract.TouringParty _order;
        public Contract.TouringParty Order
        {
            get { return _order; }
            set
            {
                _order = value;
                onOrderChanged();
            }
        }

        private void onOrderChanged()
        {
            if (Order == null)
            {
                ControlExtensions.ClearValue(
                    this.txt_Name,
                    this.txt_Customer,
                    this.txt_CPerson,
                    this.txt_CPersonTel,
                    this.txt_OrderUser,
                    this.dte_OrderDate,
                    this.txt_OrderRef,
                    this.txt_OrderRemark,
                    this.txt_OrderTotalPrice,
                    this.txt_OrderTotalVendorPrice,
                    this.txt_OrderTotalProfit,
                    this.spe_AdultQty,
                    this.spe_ActAverageAdultPrice,
                    this.spe_ActAverageChildPrice,
                    //this.spe_AveragePrice,
                    this.spe_ChildQty,
                    //this.txt_ChildType,
                    this.txt_DocumentName,
                    this.spe_AverageAdultPrice,
                    this.spe_AverageAdultVendorPrice,
                    this.spe_AverageChildPrice,
                    this.spe_AverageChildVendorPrice,
                    this.txt_SalesMan,
                    this.txt_ProvideTicketUser,
                    this.txt_Status,
                    this.chk_ShowOnWeb
                    );
                this.bds_TouringPartyItem.BindingData(null);
            }
            else
            {
                this.txt_Name.SetText(Order.Name);
                this.txt_Customer.SetText(Order.Customer);
                this.txt_CPerson.SetText(Order.ContactPerson);
                this.txt_CPersonTel.SetText(Order.ContactPersonTel);
                this.txt_OrderUser.SetText(Order.OrderUser);
                this.dte_OrderDate.SetValue(Order.OrderDate);
                this.txt_OrderRef.SetText(Order.Ref);
                this.txt_OrderRemark.SetText(Order.Remark);
                this.txt_OrderTotalPrice.SetText(Order.TotalPrice);
                this.txt_OrderTotalVendorPrice.SetText(Order.TotalVendorPrice);
                this.txt_OrderTotalProfit.SetText(Order.TotalProfit);
                this.spe_AdultQty.SetValue(Order.AdultQty);
                //this.spe_AveragePrice.SetValue(Order.AveragePrice);
                this.spe_ChildQty.SetValue(Order.ChildQty);
                //this.txt_ChildType.SetText(Order.ChildType);
                this.txt_DocumentName.SetText(Order.DocumentName);

                this.txt_SalesMan.SetText(Order.SalesMan);
                this.txt_ProvideTicketUser.SetText(Order.ProvideTicketUser);

                this.spe_AverageAdultPrice.SetValue(Order.AverageAdultPrice);
                this.spe_ActAverageAdultPrice.SetValue(Order.ActAverageAdultPrice);
                this.spe_AverageAdultVendorPrice.SetValue(Order.AverageAdultVendorPrice);
                this.spe_AverageChildPrice.SetValue(Order.AverageChildPrice);
                this.spe_ActAverageChildPrice.SetValue(Order.ActAverageChildPrice);
                this.spe_AverageChildVendorPrice.SetValue(Order.AverageChildVendorPrice);
                this.chk_ShowOnWeb.SetValue(Order.IsShowOnWeb);

                this.bds_TouringPartyItem.BindingData(Order.Items);

                this.txt_Status.SetText(Order.Status);
            }
        }
        #endregion

        private void writeFile(string fileName, byte[] file)
        {
            using (FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
            {
                fileStream.Write(Order.Document, 0, Order.Document.Length);
                fileStream.Flush();
            }
        }
    }
}
